iT邦幫忙

2024 iThome 鐵人賽

DAY 28
0
Python

Python和R入門語法比較系列 第 28

14 [R] for迴圈 和 ggplot 畫線圖 [16th 鐵人 Day 28]

  • 分享至 

  • xImage
  •  

Windows作業系統

Mac作業系統

14 for迴圈和ggplot畫圖

for 迴圈

假設我們要列出如下圖的樣子:

2024/09/01
2024/09/02
2024/09/03
2024/09/04
2024/09/05

使用cat()

cat('2024/09/0',1)
cat('2024/09/0',2)
cat('2024/09/0',3)
cat('2024/09/0',4)
cat('2024/09/0',5)

使用cat(, sep=' ')

cat('2024/09/0',1, sep='')
cat('2024/09/0',2)
cat('2024/09/0',3)
cat('2024/09/0',4)
cat('2024/09/0',5)
cat('2024/09/0',1, sep='')
cat('2024/09/0',2, sep='')
cat('2024/09/0',3, sep='')
cat('2024/09/0',4, sep='')
cat('2024/09/0',5, sep='')

使用for 迴圈 way 1: for (i in 1:5){

}

for (i in 1:5){
  print(i)
}

使用for 迴圈 way 1: for (i in 1:5){

cat()

}

for (i in 1:5){
  cat('2024/09/0',i, sep='')
}
for (i in 1:5){
  cat('2024/09/0',i, sep='')
  cat('\n')
}

as.Date 日期格式

as.Date('2024/09/01',format='%Y/%m/%d')

讀檔

setwd('/Users/carplee/Desktop/files/IT python')
r = read.csv('data/volcano_utf8.csv')

https://ithelp.ithome.com.tw/upload/images/20240923/20162398CPkXv3nmWJ.png

看看有哪些欄位

colnames(r)

[1] "日期" "數量"

載入ggplot2

library(ggplot2)

畫線圖

x=r$日期
y=r$數量
ggplot(r, aes(x,y))+
  geom_line()

https://ithelp.ithome.com.tw/upload/images/20240923/201623989ckSKlxf9g.png

看日期欄的型態

class(r$日期)

[1] "integer"

as.Date處理日期

r$日期=as.character(r$日期)
d = as.Date(r$日期,"%Y%m%d")
  [1] "2023-01-01" "2023-01-02" "2023-01-03" "2023-01-04"
  [5] "2023-01-05" "2023-01-06" "2023-01-07" "2023-01-08"
  [9] "2023-01-09" "2023-01-10" "2023-01-11" "2023-01-12"
 [13] "2023-01-13" "2023-01-14" "2023-01-15" "2023-01-16"
 [17] "2023-01-17" "2023-01-18" "2023-01-19" "2023-01-20"
 [21] "2023-01-21" "2023-01-22" "2023-01-23" "2023-01-24"
 [25] "2023-01-25" "2023-01-26" "2023-01-27" "2023-01-28"
 [29] "2023-01-29" "2023-01-30" "2023-01-31" "2023-02-01"
 [33] "2023-02-02" "2023-02-03" "2023-02-04" "2023-02-05"
 [37] "2023-02-06" "2023-02-07" "2023-02-08" "2023-02-09"
 [41] "2023-02-10" "2023-02-11" "2023-02-12" "2023-02-13"
 [45] "2023-02-14" "2023-02-15" "2023-02-16" "2023-02-17"
 [49] "2023-02-18" "2023-02-19" "2023-02-20" "2023-02-21"
 [53] "2023-02-22" "2023-02-23" "2023-02-24" "2023-02-25"
 [57] "2023-02-26" "2023-02-27" "2023-02-28" "2023-03-01"
 [61] "2023-03-02" "2023-03-03" "2023-03-04" "2023-03-05"
 [65] "2023-03-06" "2023-03-07" "2023-03-08" "2023-03-09"
 [69] "2023-03-10" "2023-03-11" "2023-03-12" "2023-03-13"
 [73] "2023-03-14" "2023-03-15" "2023-03-16" "2023-03-17"
 [77] "2023-03-18" "2023-03-19" "2023-03-20" "2023-03-21"
 [81] "2023-03-22" "2023-03-23" "2023-03-24" "2023-03-25"
 [85] "2023-03-26" "2023-03-27" "2023-03-28" "2023-03-29"
 [89] "2023-03-30" "2023-03-31" "2023-04-01" "2023-04-02"
 [93] "2023-04-03" "2023-04-04" "2023-04-05" "2023-04-06"
 [97] "2023-04-07" "2023-04-08" "2023-04-09" "2023-04-10"
[101] "2023-04-11" "2023-04-12" "2023-04-13" "2023-04-14"
[105] "2023-04-15" "2023-04-16" "2023-04-17" "2023-04-18"
[109] "2023-04-19" "2023-04-20" "2023-04-21" "2023-04-22"
[113] "2023-04-23" "2023-04-24" "2023-04-25" "2023-04-26"
[117] "2023-04-27" "2023-04-28" "2023-04-29" "2023-04-30"
[121] "2023-05-01" "2023-05-02" "2023-05-03" "2023-05-04"
[125] "2023-05-05" "2023-05-06" "2023-05-07" "2023-05-08"
[129] "2023-05-09" "2023-05-10" "2023-05-11" "2023-05-12"
[133] "2023-05-13" "2023-05-14" "2023-05-15" "2023-05-16"
[137] "2023-05-17" "2023-05-18" "2023-05-19" "2023-05-20"
[141] "2023-05-21" "2023-05-22" "2023-05-23" "2023-05-24"
[145] "2023-05-25" "2023-05-26" "2023-05-27" "2023-05-28"
[149] "2023-05-29" "2023-05-30" "2023-05-31" "2023-06-01"
[153] "2023-06-02" "2023-06-03" "2023-06-04" "2023-06-05"
[157] "2023-06-06" "2023-06-07" "2023-06-08" "2023-06-09"
[161] "2023-06-10" "2023-06-11" "2023-06-12" "2023-06-13"
[165] "2023-06-14" "2023-06-15" "2023-06-16" "2023-06-17"
[169] "2023-06-18" "2023-06-19" "2023-06-20" "2023-06-21"
[173] "2023-06-22" "2023-06-23" "2023-06-24" "2023-06-25"
[177] "2023-06-26" "2023-06-27" "2023-06-28" "2023-06-29"
[181] "2023-06-30" "2023-07-01" "2023-07-02" "2023-07-03"
[185] "2023-07-04" "2023-07-05" "2023-07-06" "2023-07-07"
[189] "2023-07-08" "2023-07-09" "2023-07-10" "2023-07-11"
[193] "2023-07-12" "2023-07-13" "2023-07-14" "2023-07-15"
[197] "2023-07-16" "2023-07-17" "2023-07-18" "2023-07-19"
[201] "2023-07-20" "2023-07-21" "2023-07-22" "2023-07-23"
[205] "2023-07-24" "2023-07-25" "2023-07-26" "2023-07-27"
[209] "2023-07-28" "2023-07-29" "2023-07-30" "2023-07-31"
[213] "2023-08-01" "2023-08-02" "2023-08-03" "2023-08-04"
[217] "2023-08-05" "2023-08-06" "2023-08-07" "2023-08-08"
[221] "2023-08-09" "2023-08-10" "2023-08-11" "2023-08-12"
[225] "2023-08-13" "2023-08-14" "2023-08-15" "2023-08-16"
[229] "2023-08-17" "2023-08-18" "2023-08-19" "2023-08-20"
[233] "2023-08-21" "2023-08-22" "2023-08-23" "2023-08-24"
[237] "2023-08-25" "2023-08-26" "2023-08-27" "2023-08-28"
[241] "2023-08-29" "2023-08-30" "2023-08-31" "2023-09-01"
[245] "2023-09-02" "2023-09-03" "2023-09-04" "2023-09-05"
[249] "2023-09-06" "2023-09-07" "2023-09-08" "2023-09-09"
[253] "2023-09-10" "2023-09-11" "2023-09-12" "2023-09-13"
[257] "2023-09-14" "2023-09-15" "2023-09-16" "2023-09-17"
[261] "2023-09-18" "2023-09-19" "2023-09-20" "2023-09-21"
[265] "2023-09-22" "2023-09-23" "2023-09-24" "2023-09-25"
[269] "2023-09-26" "2023-09-27" "2023-09-28" "2023-09-29"
[273] "2023-09-30" "2023-10-01" "2023-10-02" "2023-10-03"
[277] "2023-10-04" "2023-10-05" "2023-10-06" "2023-10-07"
[281] "2023-10-08" "2023-10-09" "2023-10-10" "2023-10-11"
[285] "2023-10-12" "2023-10-13" "2023-10-14" "2023-10-15"
[289] "2023-10-16" "2023-10-17" "2023-10-18" "2023-10-19"
[293] "2023-10-20" "2023-10-21" "2023-10-22" "2023-10-23"
[297] "2023-10-24" "2023-10-25" "2023-10-26" "2023-10-27"
[301] "2023-10-28" "2023-10-29" "2023-10-30" "2023-10-31"
> 

處理好日期後再畫一次圖

ggplot(r, aes(d, y))+
  geom_line()

https://ithelp.ithome.com.tw/upload/images/20240923/20162398eisMlay7k4.png

調整x軸的 日期格式1: 每月顯示

ggplot(r, aes(d, y))+
  geom_line()+
  scale_x_date(date_breaks = "months")

https://ithelp.ithome.com.tw/upload/images/20240923/201623981osJ99vECs.png

調整x軸的 日期格式2: 顯示年-月

ggplot(r, aes(d, y))+
  geom_line()+
  scale_x_date(date_breaks = "months",date_labels = "%Y-%m")

https://ithelp.ithome.com.tw/upload/images/20240923/20162398wEEn4xVv4Z.png

調整x軸的 日期格式3: 旋轉30度

ggplot(r, aes(d, y))+
  geom_line()+
  scale_x_date(date_breaks = "months",date_labels = "%Y-%m")+
  theme(axis.text.x=element_text(angle = 30))

https://ithelp.ithome.com.tw/upload/images/20240923/20162398VfQr8hY6Kf.png

內容預告:

15 for 迴圈 和 html網頁資料解析


上一篇
14 [Python] for迴圈 和 matplotlib.pyplot 畫線圖 [16th 鐵人 Day 27]
下一篇
15 [Python] for 迴圈 和 html網頁資料解析 by bs4套件(BeautifulSoup) [16th 鐵人 Day 29]
系列文
Python和R入門語法比較30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言